<template>
  <div class="history-container" :class="{ 'elderly-mode': elderlyModeEnabled }">
    <div class="history-header">
      <van-nav-bar
        title="历史记录"
        left-arrow
        right-text="清除全部"
        @click-left="goBack"
        @click-right="confirmClearAll"
      />
    </div>
    
    <div class="history-content">
      <div v-if="chatHistory.length === 0" class="empty-history">
        <p>暂无历史记录</p>
      </div>
      
      <div v-else class="history-list">
        <van-swipe-cell 
          v-for="session in chatHistory" 
          :key="session.id"
        >
          <div 
            class="history-item"
            @click="goToChat(session.id)"
          >
            <div class="history-info">
              <h3>{{ session.title }}</h3>
              <p class="model-name">{{ getModelName(session.model) }}</p>
              <p class="message-count">{{ session.messages.length }} 条消息</p>
            </div>
            
            <div class="history-time">
              <p>{{ formatDate(session.updatedAt) }}</p>
            </div>
          </div>
          
          <template #right>
            <van-button 
              type="danger" 
              square 
              text="删除" 
              class="delete-button"
              @click.stop="deleteSession(session.id)"
            />
          </template>
        </van-swipe-cell>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useChatStore } from '../store/chat'
import { storeToRefs } from 'pinia'

const router = useRouter()
const chatStore = useChatStore()
const { chatHistory, availableModels, elderlyModeEnabled } = storeToRefs(chatStore)

// 获取模型名称
function getModelName(modelId: string): string {
  const model = availableModels.value.find(m => m.id === modelId)
  return model ? model.name : modelId
}

// 格式化日期
function formatDate(timestamp: number): string {
  const date = new Date(timestamp)
  const now = new Date()
  const yesterday = new Date(now)
  yesterday.setDate(yesterday.getDate() - 1)
  
  // 如果是今天
  if (date.toDateString() === now.toDateString()) {
    return '今天 ' + date.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' })
  }
  
  // 如果是昨天
  if (date.toDateString() === yesterday.toDateString()) {
    return '昨天 ' + date.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' })
  }
  
  // 其他日期
  return date.toLocaleDateString('zh-CN', { 
    month: 'numeric', 
    day: 'numeric',
    hour: '2-digit', 
    minute: '2-digit'
  })
}

// 返回上一页
function goBack() {
  router.push('/')
}

// 进入聊天
function goToChat(sessionId: string) {
  const session = chatHistory.value.find(s => s.id === sessionId)
  if (session) {
    chatStore.switchSession(sessionId)
    router.push('/')
  }
}

// 删除会话
function deleteSession(sessionId: string) {
  if (confirm('确定要删除这个对话吗？')) {
    chatStore.deleteSession(sessionId)
  }
}

// 确认清除所有历史记录
function confirmClearAll() {
  if (confirm('确定要清除所有历史记录吗？此操作不可恢复。')) {
    chatStore.clearAllHistory()
    // 清除后返回聊天页面
    router.push('/')
  }
}
</script>

<style scoped>
.history-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  background-color: #f7f7f7;
}

.history-content {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
}

.empty-history {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  color: #999;
}

.history-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.history-item {
  display: flex;
  justify-content: space-between;
  background-color: #fff;
  border-radius: 8px;
  padding: 16px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  cursor: pointer;
  transition: all 0.3s ease;
}

.history-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 3px 8px rgba(0, 0, 0, 0.15);
}

.history-info {
  flex: 1;
}

.history-info h3 {
  margin: 0 0 8px 0;
  font-size: 16px;
  font-weight: 500;
}

.model-name {
  color: #666;
  font-size: 14px;
  margin: 0 0 4px 0;
}

.message-count {
  color: #999;
  font-size: 12px;
  margin: 0;
}

.history-time {
  color: #999;
  font-size: 12px;
  text-align: right;
  min-width: 80px;
}

.delete-button {
  height: 100%;
}

/* 适老化模式特定样式 */
.elderly-mode .history-item {
  padding: 16px;
  border-radius: 12px;
}

.elderly-mode .history-title {
  font-size: 20px;
  margin-bottom: 8px;
}

.elderly-mode .history-meta {
  font-size: 16px;
}

.elderly-mode .history-time {
  font-size: 16px;
}

.elderly-mode .empty-history {
  font-size: 18px;
}

.elderly-mode .van-swipe-cell__right {
  height: 100%;
}

.elderly-mode .delete-btn {
  height: 100%;
  font-size: 18px;
  padding: 0 24px;
}
</style> 